﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSharp_Form_Event
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            //public delegate void EventHandler(object sender, EventArgs e);
            //事件关联  public event EventHandler Click;
            /*this.btn1.Click += new System.EventHandler(this.btn_Click);
            this.btn2.Click += new System.EventHandler(this.btn_Click);
            this.btn3.Click += new System.EventHandler(this.btn_Click);*/

            EventHandle(true);
        }

        public void EventHandle(bool isAdd)
        {
            foreach (Control item in this.Controls)
            {
                //首先判断Tag不能为空，然后是否包含指定字符（用来筛选按钮）
                if (item.Tag != null && item.Tag.ToString().Contains("btn"))
                {
                    //MessageBox.Show(item.Tag.ToString());
                    if(isAdd)
                        item.Click += new System.EventHandler(this.btn_Click);
                    else
                        item.Click -= new System.EventHandler(this.btn_Click);
                }
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            //MessageBox.Show(btn.Text);
            //通过tag属性获取控件的数据
            string data = btn.Tag.ToString();
            MessageBox.Show(data.Substring(data.IndexOf("_") + 1));
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("上位机大佬");
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("上位机高手");
        }

        private void btn3_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("上位机菜鸟");
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        //关联事件
        private void btnAdd_Click(object sender, EventArgs e)
        {
            EventHandle(true);
        }

        //移除事件

        private void btnDelete_Click(object sender, EventArgs e)
        {
            EventHandle(false);
        }

        //创建子窗体
        private void button2_Click(object sender, EventArgs e)
        {
            //FormChild frm = new FormChild();
            FormChild frm = new FormChild("这个是新创建的子窗体");
            frm.Show();
        }

        //创建模态窗体
        private void btnCreate2_Click(object sender, EventArgs e)
        {
            FormChild frm = new FormChild("这个是新创建的子窗体");
            frm.ShowDialog(); //以模态方式显示（就是这个窗体不关闭，你无法操作其他的)
        }

        //事件响应方法
        /*private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("我们正在学习上位机客户端程序开发！事件机制","提示信息"
                ,MessageBoxButtons.OK,MessageBoxIcon.Question);
        }*/

        /*private void button1_Click_1(object sender, EventArgs e)
        {

        }*/
    }
}
